package grapher.ui;



import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.event.MouseInputAdapter;
import javax.swing.text.ZoneView;

public class EcouteurDeSouris extends MouseInputAdapter implements MouseWheelListener{
	
	Grapher g;
	int oldX, oldY, button,signeDrag=1;
	
	
	Toolkit tk = Toolkit.getDefaultToolkit();
	Image zoomIn = tk.getImage("image/Zoom-In.png");
	Image zoomOut = tk.getImage("image/Zoom-Out.png");
	Cursor zIn = tk.createCustomCursor(zoomIn, new Point(16, 16), "zoom in");
	Cursor zOut = tk.createCustomCursor(zoomOut, new Point(16, 16), "zoom Out");

	
	public EcouteurDeSouris(Grapher g) {
		this.g= g;
	}

	public void mousePressed(MouseEvent e) {
		button=e.getButton();
		switch (e.getButton()) {
			case (MouseEvent.BUTTON1) :
				oldX=e.getX();
				oldY=e.getY();
				
				g.setCursor(zIn);
				break;
			case (MouseEvent.BUTTON2) :
				break;
			case (MouseEvent.BUTTON3) :
				g.p1 = e.getPoint();
				g.p2= null; 
				g.isSelection=true;
				g.setCursor(zOut);
				break;
			default :
			
				break;
		}
	}
	
	public void mouseDragged(MouseEvent e) {
		switch (button) {
			case (MouseEvent.BUTTON1) :
				g.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
				g.translate(e.getX()-oldX, e.getY()-oldY);
				oldX=e.getX();
				oldY=e.getY();
				break;
			case (MouseEvent.BUTTON2) :
			
		        if (( e.getY()-oldY)>0) 
		        		signeDrag = 1;
		        else  signeDrag = -1; 
			  
				g.zoom(e.getPoint(), -5 * (signeDrag) );
				oldY=e.getY();
				break;
			case (MouseEvent.BUTTON3) :
			
				g.p2 = e.getPoint();
				g.repaint();
				break;
			default :
				break;
		}
	}
	

	public void mouseClicked(MouseEvent e)
	{
		switch (button) {
		case (MouseEvent.BUTTON1) :
	    
			g.zoom(e.getPoint(), 5 );
			
			break;
		case (MouseEvent.BUTTON2) :
			break;
		case (MouseEvent.BUTTON3) :
			
			g.zoom(e.getPoint(), -5 );
			break;
		default :
			break;
		}
	}
	

	public void mouseReleased(MouseEvent e) {
		switch (button) {
		case (MouseEvent.BUTTON1) :
			g.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			break;
		case (MouseEvent.BUTTON2) :
			break;
		case (MouseEvent.BUTTON3) :
			g.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			g.isSelection=false;
	    	if ( g.p2 != null) 
				g.zoom(g.p1,g.p2);
	    	
			break;
			
		default :
		
			break;
		}
	}
	
	public void mouseWheelMoved(MouseWheelEvent e) {
		g.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		g.zoom(e.getPoint(), -5*e.getWheelRotation());
	}

}